package ofpof.domain.dao.mapper;

import java.util.Date;

import ofpof.domain.GeoLocation;
import ofpof.domain.Issue;
import ofpof.domain.User;

import org.bson.types.ObjectId;
import org.springframework.stereotype.Component;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

@Component
public class IssueMapper implements MongoMapper<Issue> {

	@Override
	public Issue fromDbObject(DBObject dbObject) {
		Issue issue = new Issue();
		issue.setCreatedOn((Date) dbObject.get("createdon"));
		issue.setDescription((String) dbObject.get("description"));
		
		GeoLocation geoLocation = new GeoLocation();
		geoLocation.setLat((Double) dbObject.get("lat"));
		geoLocation.setLon((Double) dbObject.get("lon"));
		issue.setGeolocation(geoLocation);
		
		issue.setId((ObjectId) dbObject.get("_id"));
		issue.setContentPath((String) dbObject.get("contentpath"));
		
		User user = new User();
		user.setId((ObjectId) dbObject.get("userid"));
		issue.setUser(user);
		
		return issue;
	}

	@Override
	public DBObject toDbObject(Issue entity) {
		DBObject dbObject = new BasicDBObject();
		dbObject.put("_id",  entity.getId());
		dbObject.put("description", entity.getDescription());
		dbObject.put("lat", entity.getGeolocation().getLat());
		dbObject.put("lon", entity.getGeolocation().getLon());
		dbObject.put("createdon", entity.getCreatedOn());
		dbObject.put("contentpath", entity.getContentPath());
		dbObject.put("userid",entity.getUser().getId());
		return dbObject;
	}

}
